<?php
error_reporting(E_ALL);
$relPath='../../pinc/';
require_once $relPath."dpinit.php";
include_once($relPath.'dp_main.inc');
include_once($relPath.'theme.inc');
include_once($relPath.'maybe_mail.inc');

$no_stats=1;
/*
$theme_args['css_data'] = "
table.listing { border-collapse:collapse; }
table.listing td { border: 1px solid #999; }
table.listing tr.e { background-color: white; }
table.listing tr.o { background-color: #ddd; }
table.listing td { padding: 5px; }
table.listing td.enabled { background-color: #9f9; text-align: center; }
table.listing td.disabled { background-color: #ddd; text-align: center; }
table.listing td.pending { background-color: #ff8; text-align: center; }
table.listing td.center { text-align: center; }
form { padding: 0; margin: 0; }

table.source { width:75%; border-collapse:collapse;
    margin-left: auto; margin-right: auto; }
table.source td { border:1px solid black; padding:5px; }
table.source td.pa {width:30%; font-weight:bold; }";
*/

$page_url = "$pm_url/manage_image_sources.php";

$action = Arg('action', 'show_sources');
$isedit = IsArg("edit");
$isenable = IsArg("enable");
$isdisable = IsArg("disable");
$isapprove = IsArg("approve");
$issave = IsArg("save_edits");
$argsource = Arg("source");
$codename = Arg("code_name");

$can_edit = $User->IsImageSourcesManager() || $User->IsSiteManager();

// Action 'update_oneshot' is the target for form submit buttons. The
// action is based on the submit button's name. Here we do the action
// and set $action to 'show_sources' to display all in one page load.
if ($action == 'update_oneshot')
{
    if ($isedit)
    {
        $action = 'edit_source';
    }

    elseif ($isenable)
    {
        $source = new ImageSource($argsource);
        $source->enable();
        $action='show_sources';
    }

    elseif ($isdisable)
    {
        $source = new ImageSource($argsource);
        $source->disable();
        $action='show_sources';
    }

    elseif ($isapprove)
    {
        $source = new ImageSource($argsource);
        $source->approve();
        $action='show_sources';
    }

    elseif ($issave)
    {
        # This handles both edits to existing sources, and the creation of new sources
        $source = new ImageSource;

        $errmsgs = '';

        $new_code_name = $codename;

        if (strlen($new_code_name) < 1) 
            $errmsgs .= _("A value for Image Source ID is required. Please enter one.") . " ";

        $new = $dpdb->SqlOneValue("
            SELECT COUNT(*)
            FROM image_sources
            WHERE code_name = '$new_code_name'");

        if (!$new)
            $source->ImageSource($codename);

        if ( !$new && !isediting )
        {
            $errmsgs .= sprintf(_('An image source with this ID already exists. If you
            wish to edit the details of an existing source, please contact %s.
            Otherwise, choose a different ID for this source.'),$db_requests_email_addr) . "<br />";
        }

        $source->save_from_post();
        if ($can_edit)
        {
            $action = 'show_sources';
        }
        else
        {
            theme_header();
                if ($new)
                    $source->log_request_for_approval($pguser);
                echo _("Your proposal has been successfully recorded. You will be
                    notified by email once it has been approved.");
            theme_footer();
        }

    }
}

if ($action == 'show_sources')
{
    // The more detailed listing of image sources is only available
    // to managers.
    if (!$can_edit)
        do_redirect("$pm_url/show_image_sources.php");

    theme_header(_('List Image Sources'));

    show_is_toolbar();

    $rows = $dpdb->SqlRows("
        SELECT code_name FROM image_sources 
        ORDER BY display_name ASC");

    echo "
        <br />
    <table class='dptable w70 center light_border'>
    <tr>
    <th>" . _("ID") . "</th>
    <th>" . _("Display Name") . "</th>
    <th>" . _("Full Name") . "</th>
    <th>" . _("Status") . "</th>
    <th>" . _("Images<br>stored") . "</th>
    <th>" . _("Images<br>published") . "</th>
    <th>" . _("Source shown to") . "</th>
    <th></th>
    </tr>\n";

    $count = 0;
    foreach($rows as $row) {
        $count++;
        $source_name = $row['code_name'];

        $source = new ImageSource($source_name);
        $source->show_listing_row($count);
    }
    echo "</table>";
    echo "<br>";

    theme_footer();
}

elseif ($action == 'edit_source')
{
    $source = new ImageSource($argsource);
    theme_header(sprintf(_("Editing %s"),$source->display_name));
    show_is_toolbar();
    $source->show_edit_form();
    theme_footer();
}

elseif ($action == 'add_source')
{
    $title = $can_edit ? _('Add a new image source') : _('Propose a new image source');
    theme_header($title);
    show_is_toolbar();
    $blank = new ImageSource(null);
    $blank->show_edit_form();
    theme_footer();
}


// ----------------------------------------------------------------------------

class ImageSource
{
    private $code_name;
    private $new_source;

    function __construct($code_name = null)
    {
        global $dpdb;

        $this->_code_name = $code_name;
        if( !is_null($code_name) )
        {
            $source_fields = $dpdb->SqlOneRow("
                SELECT *
                FROM image_sources
                WHERE code_name = '$code_name'");

            foreach ($source_fields as $field => $value)
            {
                $this->$field = $value;
            }
            $this->new_source = false;
        }
        else
        {
            $this->new_source = true;
        }
    }

    function show_listing_row($count)
    {
        global $page_url;
        global $form_enctype;
        $sid = $this->code_name;

        if($count%2 == "1")
            $row_class = "o";
        else
            $row_class = "e";

        // calculate how many rows this listing will have so we can span them
        // for some columns
        $listing_rows = 2;
        if($this->public_comment)
            $listing_rows++;
        if($this->internal_comment)
            $listing_rows++;

        echo "<tr class='$row_class'>";
        echo "<td rowspan='$listing_rows'>" . ($this->code_name) . "</td>";
        echo "<td><a name='$sid' id='$sid'></a>" . ($this->display_name) . "</td>";
        echo "<td>" . make_link($this->url, $this->full_name) . "</td>";
        echo $this->_get_status_cell($this->is_active,' pb');
        echo "<td class='center'>" . $this->_may_maynot_unknown($this->ok_keep_images) . "</td>";
        echo "<td class='center'>" . $this->_may_maynot_unknown($this->ok_show_images) . "</td>";
        echo "<td>" . $this->_showto($this->info_page_visibility) . "</td>";
        echo "<td class='center' rowspan='$listing_rows'>";
        echo "<form method='post' accept-charset='UTF-8'
                    action='$page_url'>";
        echo "  <input type='hidden' name='action' value='update_oneshot'>\n";
        echo "  <input type='hidden' name='source' value='$sid'>\n";
                $this->show_buttons();
        echo "</form>";
        echo "</td>";
        echo "</tr>";

        echo "<tr class='$row_class'>";
        echo "<td colspan='6'>" . sprintf(_("<b>Credits Line:</b> %s"), ($this->credit)) . "</td>";
        echo "</tr>";

        if($this->public_comment)
        {
            echo "<tr class='$row_class'>";
            echo "<td colspan='6'>" . sprintf(_("<b>Comment (public):</b> %s"), ($this->public_comment)) . "</td>";
            echo "</tr>";
        }

        if($this->internal_comment)
        {
            echo "<tr class='$row_class'>";
            echo "<td colspan='6'>" . sprintf(_("<b>Notes (internal):</b> %s"), ($this->internal_comment)) . "</td>";
            echo "</tr>";
        }
    }

    function show_buttons()
    {
        echo "<input type='submit' name='edit' value='".attr_safe(_('Edit'))."' /> ";
        switch ($this->is_active)
        {
            case('-1'):
                echo "<input type='submit' name='approve' value='".attr_safe(_('Approve'))."' /> ";
                break;
            case('0'):
                echo "<input type='submit' name='enable' value='".attr_safe(_('Enable'))."' /> ";
                break;
            case('1'):
                echo "<input type='submit' name='disable' value='".attr_safe(_('Disable'))."' /> ";
                break;
        }
    }

    function show_edit_form()
    {
        global $page_url;
        echo "<table class='dptable'>
        <form method='post' accept-charset='UTF-8'
            action='$page_url?action=update_oneshot#$this->code_name'>\n";

        if($this->new_source)
        {
            $this->_show_edit_row('code_name',_('Image source ID'),false,10);
        }
        else
        {
            echo "<input type='hidden' name='editing' value='true' />" .
                "<input type='hidden' name='code_name' value='" . ($this->code_name) ."' />";
            $this->_show_summary_row(_('Image source ID'),$this->code_name);
        }
        $this->_show_edit_row('display_name',_('Display name'),false,30);
        $this->_show_edit_row('full_name',_('Full name'));
        $this->_show_edit_row('url',_('Web site'));
        $this->_show_edit_row('credit',_('Credits line'),true);
        $this->_show_edit_permissions_row();
        $this->_show_edit_row('public_comment',_('Comment (public)'),true);
        $this->_show_edit_row('internal_comment',_('Notes (internal)'),true);

        echo "<tr><td colspan='2' style='text-align:center;'>
            <input type='submit' name='save_edits' value='".attr_safe(_('Save'))."' />
            </td> </tr> </form> </table>\n\n";
    }

    function _show_edit_row($field, $label, $textarea = false, $maxlength = null)
    {

        $value = $this->new_source
            ? (empty($_REQUEST[$field]) ? '' : $_REQUEST[$field])
            : $this->$field;

        $value = ($value);

        if ($textarea)
        {
            $editing = "<textarea cols='60' rows='6' name='$field'>$value</textarea>";
        }
        else
        {
            $maxlength_attr = is_null($maxlength) ? '' : "maxlength='$maxlength'";
            $editing = "<input type='text' name='$field' size='60' value='$value' $maxlength_attr />";
        }
        echo "  <tr>" .
            "<td class='pa'>$label</td>" .
            "<td class='pb'>$editing</td>" .
            "</tr>\n";
    }

    function _show_edit_permissions_row()
    {
        global $site_abbreviation;
        $cols = array(
            array('field' => 'ok_keep_images', 'label' => _('Images may be stored'), 'allow_unknown' => true),
            array('field' => 'ok_show_images', 'label' => _('Images may be published'), 'allow_unknown' => true),

            );

        $editing = '';

        foreach ($cols as $col)
        {

            $field = $col['field'];
            $existing_value = $this->new_source
                ? (empty($_REQUEST[$field]) ? '-1' : $_REQUEST[$field])
                : $this->$field;

            $editing .= "$col[label] <select name='$col[field]'>";
            foreach (array('1' => _('Yes'),'0' => _('No'),'-1' => _('Unknown')) as $val => $opt)
            {
                if (! (!$col['allow_unknown'] && $val == -1) )
                {
                $editing .= "<option value='$val' " .
                    ($existing_value == $val ? 'selected' :'') .
                    ">$opt</option>";
                }
            }
            $editing .= "</select><br />";
        }

        // info page visibility is more complicated
        //  0 = Image Source Managers and SAs
        //  1 = also any PM
        //  2 = also any logged-in user
        //  3 = anyone

            $field = 'info_page_visibility';
            $existing_value = $this->new_source
                ? (empty($_REQUEST[$field]) ? '2' : $_REQUEST[$field])
                : $this->$field;

            $editing .= _("Visibility on Info Page") . " <select name='$field'>";
            foreach (array(
                    '0' => _('IS Managers Only'),
                    '1' => _('Also PMs'),
                    // TRANSLATORS: %s is the site abbreviation
                    '2' => sprintf(_("All %s Users"), $site_abbreviation),
                    '3' => _('Publicly Visible')) 
                as $val => $opt)
            {
                {
                $editing .= "<option value='$val' " .
                    ($existing_value == $val ? 'selected' :'') .
                    ">$opt</option>";
                }
            }
            $editing .= "</select><br />";


        $this->_show_summary_row(_('Permissions'),$editing,false);
    }

    function save_from_post()
    {
        global $errmsgs,$can_edit,$new;
        global $dpdb;
        $std_fields_sql = "";
        $std_fields = array('display_name','full_name','credit',
                    'ok_keep_images','ok_show_images','info_page_visibility','public_comment','internal_comment');
        foreach ($std_fields as $field)
        {
            $this->$field = $_POST[$field];
            $std_fields_sql .= "$field = '{$this->$field}',\n";
        }

        // If the url has no scheme, prepend http://
        $this->url = strpos($_POST['url'],'://') ? $_POST['url'] : 'http://'.$_POST['url'];
        $this->code_name = strtoupper($_POST['code_name']);
        if ($new)
        {
            // If the user is an image sources manager, then the new source should
            // default to disabled. If not, the source should default to pending approval.
            $this->is_active = $can_edit ? '0' : '-1';
            // new sources shouldn't be shown on
            // the public version of the info page until they are approved.
             $this->info_page_visibility = '1' ;
        }

        if ($errmsgs)
        {
            theme_header();
            echo "<p style='font-weight: bold; color: red;'>" . $errmsgs . "</p>";
            $this->show_edit_form();
            theme_footer();
            die;
        }

        $dpdb->SqlExecute("
            REPLACE INTO image_sources
            SET
                code_name = '$this->code_name',
                $std_fields_sql
                url = '$this->url',
                is_active = '$this->is_active'");

    }

    function enable()
    {
        $this->_set_field('is_active',1);
    }

    function disable()
    {
        $this->_set_field('is_active',0);
    }


    function approve()
    {
        global  $pguser,
                $site_url,
                $site_abbreviation,
                $site_signoff,
                $site_name;
        $this->_set_field('is_active',1);
        $this->_set_field('info_page_visibility',1);

        $row = $dpdb->OneRow("
            SELECT users.username,users.email
            FROM usersettings,users
            WHERE usersettings.setting = 'is_approval_notify'
            AND usersettings.value = '$this->code_name'
            AND usersettings.username = users.username");

        $dpdb->SqlExecute("
            DELETE FROM usersettings
            WHERE setting = 'is_approval_notify'
            AND usersettings.value = '{$this->code_name}'");

        list($username, $email) = $row;

        $subject = sprintf(_('%s: Image source %s has been approved!'),$site_abbreviation,$this->display_name);

        $body = "Hello $username,\n\n" .
            "This is a message from the $site_name website.\n\n".
            "The image source that you proposed, $this->display_name, has been\n".
            "approved by $pguser. You can select it, and apply it to projects, from\n".
            "your project manager's page.\n\n$site_signoff";

        maybe_mail($email,$subject,$body,null);
    }

    function _set_field($field,$value)
    {
        global $dpdb;
        $dpdb->SqlExecute("
            UPDATE image_sources
            SET $field = '$value'
            WHERE code_name = '$this->code_name'");
        $this->$field = $value;
    }


    function _show_summary_row($label,$value)
    {
        echo "  <tr>" .
            "<td class='pa'>$label</td>" .
            "<td class='pb'>" . $value . "</td>" .
            "</tr>\n";
    }

    function _get_status_cell($status,$class = '')
    {
        switch ($status)
        {
          case(1):
              $middle = _('Enabled');
              $open = "<td class='enabled{$class}'>";
              break;
          case(0):
              $middle = _('Disabled');
              $open = "<td class='disabled{$class}'>";
              break;
          case(-1):
              $middle = _('Pending approval');
              $open = "<td class='pending{$class}'>";
              break;
        }

        return $open . $middle . '</td>';
    }

    function _may_maynot_unknown($value)
    {
        if ($value != '-1')
        {
            return ( $value ? _('may') : _('may not') );
        }
        else
            return "unknown";
    }

    function _showto($show_to)
    {
        global $site_abbreviation;
        switch ($show_to)
        {
            case '0': return _("Image Managers Only");
            case '1':  return _("Project Managers");
            case '2':  return sprintf(_("Any %s User"),$site_abbreviation);
            case '3':  return _("All Users and Visitors");
        }
        return _("Visibility improperly specified.");
    }

    function log_request_for_approval($requestor_username)
    {
        global $dpdb;
        global $general_help_email_addr,
               $image_sources_manager_addr,
               $pm_url,
               $site_abbreviation,
               $site_name,
               $site_signoff;

        $dpdb->SqlExecute("
            INSERT INTO usersettings
            SET
                username = '$requestor_username',
                setting = 'is_approval_notify',
                value = '$this->code_name'");

        $subject = sprintf(_('%s: New image source proposed'),$site_abbreviation)." : ".$this->display_name;

        $body = "Hello,\n\nYou are receiving this email because\n".
        "you are listed as an image sources manager at the $site_name\n".
        "site. If this is an error, please contact <$general_help_email_addr>.\n\n".
        "$requestor_username has proposed that $this->display_name be added\n".
        "to the list of image sources. To edit or approve this image source,\n".
        "visit\n    $pm_url/manage_image_sources.php?action=show_sources#$this->code_name".
        "\n\n$site_signoff";

        maybe_mail($image_sources_manager_addr,$subject,$body,null);
    }

}

// ----------------------------------------------------------------------------

function make_link($url,$label)
{
    $start = substr($url,0,3);
    if ($start == 'htt')
    {
        return "<a href='$url'>$label</a>";
    }
    else
    {
        return "<a href='http://$url'>$label</a>";
    }
}

function show_is_toolbar()
{
    global $action;

    $pages = array(
        'add_source' => _('Add New Source'),
        'show_sources' => _('List All Image Sources')
    );
    echo "<p style='text-align: center; margin: 5px 0 5px 0;'>";
    foreach ($pages as $new_action => $label)
    {
        echo ($action == $new_action) ? "<b>" : "<a href='?action=$new_action'>";
        echo $label;
        echo ($action == $new_action) ? "</b>" : "</a>";

        if ( $label != end($pages) )
            echo " | ";
    }
    echo "</p>";
}

// vim: sw=4 ts=4 expandtab
?>
